package Dao.dao.impl;

import Chapter01.DBUtil;
import Dao.dao.GradeDao;
import Dao.entity.Grade;
import Dao.entity.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class GradeDaoImpl implements GradeDao {
    @Override
    public List<Grade> all() {
        List<Grade> list=new LinkedList<>();
        Connection connection = DBUtil.getConnection();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=connection.prepareStatement("select * from s_grade");
            rs=ps.executeQuery();
            while (rs.next()){
                Grade grade=new Grade();
                grade.setId(rs.getLong("id"));
                grade.setGname(rs.getString("gname"));
                list.add(grade);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(rs,ps,connection);
        }
        return list;
    }

    //增
    @Override
    public int add(Grade g) {
        Connection connection = DBUtil.getConnection();
        PreparedStatement ps=null;
        int zhi=0;
        try {
            System.out.println(connection.getClass().getName());
            System.out.println("连接已经建立");
            String sql="insert into `s_grade`(`gname`)values(?)";
            System.out.println(sql);
            ps=connection.prepareStatement(sql);
            //!!给占位符赋值
            ps.setString(1,g.getGname());
            zhi=ps.executeUpdate();
            System.out.printf("%d行被修改了",zhi);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(ps);
            DBUtil.close(connection);
        }
        return zhi;
    }

    //删
    @Override
    public int delte(int id) {
        Connection connection = DBUtil.getConnection();
        PreparedStatement ps=null;
        int zhi=0;
        try {
            System.out.println(connection.getClass().getName());
            System.out.println("连接已经建立");
            String sql="delete from s_grade where id=?";
            System.out.println(sql);
            ps=connection.prepareStatement(sql);
            //!!给占位符赋值
            ps.setLong(1,id);
            zhi=ps.executeUpdate();
            System.out.printf("%d行被修改了",zhi);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(ps);
            DBUtil.close(connection);
        }
        return zhi;
    }

    //改
    @Override
    public int update(Grade g) {
        Connection connection = DBUtil.getConnection();
        PreparedStatement ps = null;
        int zhi=0;
        try {
            System.out.println(connection.getClass().getName());
            System.out.println("连接已经建立");

            String sql = "update s_grade set gname=? where id=?";
            System.out.println(sql);
            ps = connection.prepareStatement(sql);
            //!!给占位符赋值
            ps.setString(1,g.getGname());
            ps.setLong(2,g.getId());
            zhi = ps.executeUpdate();
            System.out.printf("%d行被修改了", zhi);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(ps);
            DBUtil.close(connection);
        }
        return zhi;
    }
}
